/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.threads;

import com.hyts.algorithm.Solution;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.threads
 * @author:LiBo/Alex
 * @create-date:2019-10-31 15:47
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class TestThreads implements Solution {


    public Object object = new Object();


    @Test
    @Override
    public void solution1() {

       Thread thread1 = new Thread(()->{
            for(int i = 0;i<=100;i++){
                synchronized (object) {
                    if ((i&1)==0) {
                        System.out.println(Thread.currentThread().getName()+"-"+i);
                    }
                    try {
                        object.notify();
                        object.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        },"偶数线程");

        Thread thread2 = new Thread(()->{
            for(int i = 0;i<=100;i++){
                synchronized (object) {
                    if ((i&1) != 0) {
                        System.out.println(Thread.currentThread().getName()+"-"+i);
                    }
                    try {
                        object.notify();
                        object.wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        },"基数线程");

        try {
            thread1.start();
            thread2.start();
            //thread1.join(3000);
            thread1.join();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Test
    @Override
    public void solution2() {

        Object lock1 = new Object();
        Object lock2 = new Object();
        Object lock3 = new Object();

        Thread t1 = new Thread(()->{
            for (;;) {
                synchronized (lock2) {
                    synchronized(lock1) {
                        try {
                            System.out.println(1);
                            lock1.notifyAll();
                        } catch (Exception e) {
                                e.printStackTrace();
                       }
                    }
                    try {
                        lock2.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread t2 = new Thread(()->{
            for (;;) {
                synchronized (lock3) {
                    synchronized(lock2) {
                        try {
                            System.out.println(2);
                            lock2.notifyAll();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        lock3.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread t3 = new Thread(()->{
            for (;;) {
                synchronized (lock1) {
                    synchronized(lock3) {
                        try {
                            System.out.println(3);
                            lock3.notifyAll();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void solution3() {

    }
}
